package com.elong.android.specialhouse.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.elong.android.specialhouse.AppConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.base.BaseVolleyActivity;
import com.elong.android.specialhouse.payment.ApartmentPaymentCounterImpl;
import com.elong.android.specialhouse.request.CreateOrderReq;
import com.elong.android.specialhouse.utils.EventReportTools;
import com.elong.framework.netmid.response.IResponse;
import com.elong.payment.base.PaymentConstants;

public class BookingSubmitedActivity extends BaseVolleyActivity<IResponse<?>> {
    private static final String PAGE_NAME = "youfangSubmitSuccessPage";
    private static final int REQUEST_ACTIVITY_PAYMENT = 0;
    private TextView tvGoToPay;
    private TextView tvReturnHome;

    private String mGorderId;
    private String mOrderId;
    private String mApartmentName;
    private Intent mIntent;

    /**
     * 创建订单请求参数的model
     */
    private CreateOrderReq mCreateOrderReqParam;

    private boolean isFromBookingPage = true;

    private String mComeFrom;

    @Override
    protected void initContentView() {
        setContentView(R.layout.act_booking_submited);

        tvGoToPay = (TextView) findViewById(R.id.tv_goto_pay);
        tvReturnHome = (TextView) findViewById(R.id.tv_return_home);

        setListener();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventReportTools.sendPageOpenEvent(PAGE_NAME);
        initData();
    }

    private void setListener() {
        tvGoToPay.setOnClickListener(this);
        tvReturnHome.setOnClickListener(this);
    }

    private void initData() {
        mIntent = getIntent();
        if (mIntent.getBooleanExtra(AppConstants.KEY_IS_CONTINUE_PAY, false)) {
            //继续支付方式（从订单列表或者订单详情页）跳过来的
            isFromBookingPage = false;
            onContinuePay();
        } else {
            //正常预定-->支付流程跳过来的
            isFromBookingPage = true;
            mGorderId = mIntent.getStringExtra(AppConstants.GORDER_ID);
            mOrderId = mIntent.getStringExtra(AppConstants.ORDER_ID);
            mApartmentName = mIntent.getStringExtra(AppConstants.APARTMENT_NAME);
            mCreateOrderReqParam = (CreateOrderReq) mIntent.getSerializableExtra(AppConstants.ORDER_REQ_PARAMS);
            if (mCreateOrderReqParam == null) {
                mCreateOrderReqParam = new CreateOrderReq();
            }
        }
    }

    private void onContinuePay() {
        mComeFrom = mIntent.getStringExtra(AppConstants.BUNDLE_KEY_COME_FROM);
        mGorderId = mIntent.getStringExtra(PaymentConstants.gorderId);
        mOrderId = mIntent.getStringExtra(PaymentConstants.orderId);

        Intent intent = new Intent(this, ApartmentPaymentCounterImpl.class);
        intent.putExtra(PaymentConstants.orderId, mIntent.getStringExtra(PaymentConstants.gorderId));
        intent.putExtra(PaymentConstants.gorderId, mIntent.getStringExtra(PaymentConstants.gorderId));
        intent.putExtra(PaymentConstants.totalPrice, mIntent.getDoubleExtra(PaymentConstants.totalPrice, 0));
        intent.putExtra(PaymentConstants.hotelName, mIntent.getStringExtra(PaymentConstants.hotelName));
        intent.putExtra(PaymentConstants.descTitle, mIntent.getStringExtra(PaymentConstants.hotelName));
        intent.putExtra(PaymentConstants.descSubhead,mIntent.getStringExtra(PaymentConstants.hotelName));

        intent.putExtra(PaymentConstants.isCanback, mIntent.getBooleanExtra(PaymentConstants.isCanback, false));

//        intent.putExtra(PaymentConstants.checkinDate, mIntent.getStringExtra(PaymentConstants.checkinDate));
//        intent.putExtra(PaymentConstants.checkoutDate, mIntent.getStringExtra(PaymentConstants.checkoutDate));
//        intent.putExtra(PaymentConstants.totalsDays, mIntent.getStringExtra(PaymentConstants.totalsDays));

        startActivityForResult(intent, REQUEST_ACTIVITY_PAYMENT);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_goto_pay:
                // 去支付
                EventReportTools.sendPageSpotEvent(PAGE_NAME, "pay");//去支付pay
                goToPay();
                break;
            case R.id.tv_return_home:
                // 返回首页
                EventReportTools.sendPageSpotEvent(PAGE_NAME, "home");//返回首页home
                goToHomePage();
                break;

            default:
                break;
        }
    }

    private void goToPay() {
        Intent intent = new Intent(this, ApartmentPaymentCounterImpl.class);
        intent.putExtra(PaymentConstants.orderId, mOrderId);
        intent.putExtra(PaymentConstants.gorderId, mGorderId);
        intent.putExtra(PaymentConstants.totalPrice, Double.valueOf(mCreateOrderReqParam.sumPrice));
        intent.putExtra(PaymentConstants.weiXinProductName, "艺龙有房(公寓订单ID: " + mGorderId + ")");
        intent.putExtra(PaymentConstants.hotelName, mApartmentName);
        // TODO
//        intent.putExtra(PaymentConstants.BUNDLEKEY_PAYFROM, PaymentConstants.PAY_FROM_GROUPON);
        intent.putExtra(PaymentConstants.isCanback, false);
        // 业务定制描述信息
        intent.putExtra(PaymentConstants.descTitle, mApartmentName);
        intent.putExtra(PaymentConstants.descSubhead, mApartmentName);


//        String checkinDate = DateTimeUtils.parseStringDate(mCreateOrderReqParam.arriveDate);
//        String checkoutDate = DateTimeUtils.parseStringDate(mCreateOrderReqParam.leaveDate);
//        if (!TextUtils.isEmpty(checkinDate)) {
//            intent.putExtra(PaymentConstants.checkinDate, mCreateOrderReqParam.arriveDate);
//        }
//        if (!TextUtils.isEmpty(checkoutDate)) {
//            intent.putExtra(PaymentConstants.checkoutDate, mCreateOrderReqParam.leaveDate);
//        }
//
//        intent.putExtra(PaymentConstants.totalsDays, mCreateOrderReqParam.totalDays + "");

        startActivityForResult(intent, REQUEST_ACTIVITY_PAYMENT);
    }

    private void goToHomePage() {
        Intent intent = new Intent(this, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(AppConstants.KEY_HOME_TAB_INDEX,0);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ACTIVITY_PAYMENT) {
            switch (resultCode) {
                case RESULT_OK:
                    //支付成功
                    gotoPaySuccessPage();
                    break;
                case RESULT_CANCELED:
                    onPayCancel();
                    break;
            }
        }
    }

    private void onPayCancel() {
        if (!TextUtils.isEmpty(mComeFrom) && MyOrderDetailActivity.SELF.equals(mComeFrom)) {
            back();
        }
        else {
            // 查看订单详情
            goToOrderDetailPage();
        }
    }

    private void goToOrderDetailPage() {
        Intent intent = new Intent(this, MyOrderDetailActivity.class);
        intent.putExtra("mGorderId", mGorderId);
        intent.putExtra(AppConstants.KEY_IS_FROM_ORDER_LIST_PAGE, false);
        startActivity(intent);
        back();
    }

    private void gotoPaySuccessPage() {
        Intent intent = new Intent(this, PaySuccessActivity.class);
        intent.putExtra(AppConstants.GORDER_ID, mGorderId);
        intent.putExtra(AppConstants.ORDER_ID, mOrderId);
        startActivity(intent);
        finish();
    }
}
